define(function(require, exports, module) {
	var abpdata = require("abpinit");
	var abp = abpdata.abpstart;
	var muiTips = require("muiTips"); //封装的弹框
	//钉钉js
	require('http://g.alicdn.com/ilw/ding/0.8.9/scripts/dingtalk.js');
	//钉钉返回
	var dingTalkNav = require('dd_config');
	//ios
	dingTalkNav.LeftNavBar(clearSessionDD);
	//ad
	dingTalkNav.leftAndroid(clearSessionDD);
	//调用页面权限控制接口
	var sourceType = abp.sourceType; //获取权限类型接口数据
	var authorityInit = require("operatAuthority"); //调用封装的权限方法
	var authorityData = sessionStorage.getItem('operatAuthorityData'); //获取权限数据
	var authoritySupplier = authorityInit.getJurisdiction(authorityData, sourceType.INFO_SUPPLIER); //供应商权限判断

	$(function() {
		//得到客户Id
		var supplierId = sessionStorage.getItem("supplierId");
		//渲染页面

		getSupplierDetails(supplierId);
		//删除供应商
		mui("body").on("tap", "#delete", function() {
			var btnArray = ['否', '是'];
			mui.confirm('确认删除？', ' ', btnArray, function(e) {
				if(e.index == 1) {
					deleteSupplierDetails(supplierId);
				} else {
					//						info.innerText = 'MUI没有得到你的认可，继续加油'
				}
			});
		});

		function getRightFun() {
			var btnArray = ['否', '是'];
			mui.confirm('确认删除？', ' ', btnArray, function(e) {
				if(e.index == 1) {
					deleteSupplierDetails(supplierId);
				} else {
					//						info.innerText = 'MUI没有得到你的认可，继续加油'
				}
			});
		}
		if(authoritySupplier.indexOf(3) === -1) {
			muiTips.alertTips(' ', '您没有操作供应商的权限，请联系管理员开通吧！', 2, 3, "default");
			$('#sub-btn > a').remove();
			$('#sub-btn').hide();
			$("header > a").hide();
			dingTalkNav.RightNavBar(false, "删除", "default");
		} else {
			$('#sub-btn').show();
			$("header > a").show();
			dingTalkNav.RightNavBar(true, "删除", getRightFun);
		}

		function getSupplierDetails(supplierId) {
			//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
			var option = {};
			var dataModel = {
				id: supplierId
			};
			abp.services.app.supplier.getModel(dataModel, option).
			done(function(data) {
				//赋值
				$("#companyName").html(data.companyName);
				$("#companySerial").html(data.companySerial);
				$("#catalogName").html(data.infoCompanyCatalog.companyCatalogName);
				$("#amount").html(data.amount);
				$("#contact").prepend(data.infoContactList[0].contactName);
				$("#contactMobile").html(data.infoContactList[0].contactMobile);
				$("#contactTel").html(data.infoContactList[0].contactTel);
				$("#address").html(data.address);
				$("#notes").html(data.notes);
				//存储当前用户的参数
				mui("body").on("tap", "#save-client-data", function() {
					sessionStorage.supplierData = JSON.stringify(data);
					//			       		sessionStorage.setItem("title","修改客户");
				});
				if(data.success) {

				} else {
					//console.log(data.error.message)

				}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}

		/*删除客户*/
		function deleteSupplierDetails(supplierId) {
			//dataModel需要传的模型值,option非必要参数，默认传空对象，done异步完成，fail异步失败，data.success数值成功返回
			var option = {};
			var dataModel = {
				"id": supplierId,
				"companySerial": $("#companySerial").html()
			};
			abp.services.app.supplier.delete(dataModel, option).
			done(function(data) {
				//	            	console.log(data)
				clearSession();
				document.location.href = "../../src/chioceSupplier/chioceSupplierList.html?dd_nav_bgcolor=FFFF943E";

				if(data.success) {

				} else {
					//console.log(data.error.message)

				}
			}).fail(function(data) {
				var muiTips = require("muiTips");
				muiTips.alertTips(' ', data.message, 2, 3, "default");
			});
		}

	});
	//返回清除缓存
	mui("body").on("tap", "header>button", function() {
		clearSession();
	});

	function clearSessionDD() {
		sessionStorage.removeItem("chioceSupplierName");
		sessionStorage.removeItem("chioceSupplierNameId");
		sessionStorage.removeItem("supplierData");
		sessionStorage.removeItem("supplierId");
		document.location.href = "../../src/chioceSupplier/chioceSupplierList.html?dd_nav_bgcolor=FFFF943E";
	}

	function clearSession() {
		sessionStorage.removeItem("chioceSupplierName");
		sessionStorage.removeItem("chioceSupplierNameId");
		sessionStorage.removeItem("supplierData");
		sessionStorage.removeItem("supplierId");
	}
});